<?php

namespace app\teacher\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\enums\LearnMeansEnum;
use app\common\service\learn\LearnWordMemorizeService;

/*
 * 单词识记记录
 * */
class learnWordMemorizeRecord extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 查询课程单词统计数据
     * @param int|null $studentId 学员ID，传了就是上课模式
     * @param int $courseId 课程ID
     * @return array 统计数据
     */
    public function getTjData()
    {
        $studentId = $this->request->get('studentId');
        $courseId = $this->request->get('courseId');

        // 确定trainerId：如果studentId为空，则使用当前用户ID，否则为null
        $trainerId = !$studentId ? $this->auth->id : null;

        // 确定learnMeans：如果studentId不为空则是教学模式，否则是练习模式
        $learnMeans = $studentId ? LearnMeansEnum::E1 : LearnMeansEnum::E2; // E1=教学(1), E2=练习(2)

        // 调用业务层方法获取统计数据
        $LearnWordMemorizeService = new LearnWordMemorizeService();
        $tjData = $LearnWordMemorizeService->getTjData($trainerId, $studentId, $learnMeans, $courseId);

        $this->ok('操作成功', $tjData);
    }

    /**
     * 获取单词记忆等级数量
     * @param int|null $studentId 学员ID，传了就是上课模式
     * @param int $courseId 课程ID
     * @return array 统计数据
     */
    public function getMemorizeLevelNum()
    {
        $studentId = $this->request->get('studentId');
        $courseId = $this->request->get('courseId');

        // 确定trainerId：如果studentId为空，则使用当前用户ID，否则为null
        $trainerId = !$studentId ? $this->auth->id : null;

        // 确定learnMeans：如果studentId不为空则是教学模式，否则是练习模式
        $learnMeans = $studentId ? LearnMeansEnum::E1 : LearnMeansEnum::E2; // E1=教学(1), E2=练习(2)

        // 调用业务层方法获取统计数据
        $LearnWordMemorizeService = new LearnWordMemorizeService();
        $tjData = $LearnWordMemorizeService->getMemorizeLevelNum($trainerId, $studentId, $learnMeans, $courseId);

        $this->ok('操作成功', $tjData);
    }


    /**
     * 分页-课程学新单词
     * @param int|null $studentId 学员ID，传了就是上课模式
     * @param int $courseId 课程ID
     * @return array 统计数据
     */
    public function listNewWordPage()
    {
        $studentId = $this->request->get('studentId');
        $courseId = $this->request->get('courseId');
        $page = $this->request->get('page',1);
        $limit = $this->request->get('limit',20);

        // 确定trainerId：如果studentId为空，则使用当前用户ID，否则为null
        $trainerId = !$studentId ? $this->auth->id : null;

        // 确定learnMeans：如果studentId不为空则是教学模式，否则是练习模式
        $learnMeans = $studentId ? LearnMeansEnum::E1 : LearnMeansEnum::E2; // E1=教学(1), E2=练习(2)

        // 调用业务层方法获取统计数据
        $LearnWordMemorizeService = new LearnWordMemorizeService();
        $tjData = $LearnWordMemorizeService->getNewWord($page, $limit,$trainerId, $studentId, $learnMeans, $courseId);

        $this->ok('操作成功', $tjData);
    }


    /*
     * 提交-单词学习记录
     * */
    public function submitSuccess()
    {
        // 获取请求中的JSON数据
        $data = $this->request->post();

        // 验证必要参数
        $appointmentId = isset($data['appointmentId']) ? $data['appointmentId'] : null;
        $courseId = isset($data['courseId']) ? $data['courseId'] : 0;
        $learnWordList = isset($data['learnWordList']) ? $data['learnWordList'] : [];

        if (empty($courseId)) {
            $this->fail('课程ID不能为空');
        }

        if (empty($learnWordList)) {
            $this->fail('单词学习列表不能为空');
        }

        // 确定trainerId和studentId
        $trainerId = $this->auth->id;

        // 构造提交数据
        $submitData = [
            'appointmentId' => $appointmentId,
            'courseId' => $courseId,
            'learnWordList' => $learnWordList
        ];
//        halt($trainerId);

            // 调用业务层方法提交学习记录
        $LearnWordMemorizeService = new LearnWordMemorizeService();
        $result = $LearnWordMemorizeService->submitLearnRememberWord(
            $trainerId,
            AccountTypeEnum::TRAINER,
            $submitData
        );
        if (!$result){
            $this->fail($LearnWordMemorizeService->getError());
        }
        $this->ok('提交成功', $result);
    }

    /*
     * 查询-课程复习单词
     * */
    public function listReviewWord()
    {
        $studentId = $this->request->get('studentId');
        $courseId = $this->request->get('courseId');

        // 确定trainerId：如果studentId为空，则使用当前用户ID，否则为null
        $trainerId = !$studentId ? $this->auth->id : null;

        // 确定learnMeans：如果studentId不为空则是教学模式，否则是练习模式
        $learnMeans = $studentId ? LearnMeansEnum::E1 : LearnMeansEnum::E2; // E1=教学(1), E2=练习(2)

        // 调用业务层方法获取统计数据
        $LearnWordMemorizeService = new LearnWordMemorizeService();
        $tjData = $LearnWordMemorizeService->getReviewWord($trainerId, $studentId, $learnMeans, $courseId);

        $this->ok('操作成功', $tjData);
    }

}